<template>
    <div class="login-container">
        <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            label-position="left"
        >
            <div class="title-container">
                <h3 class="title">XX后台管理模板</h3>
            </div>

            <!-- 账号 -->
            <el-form-item prop="username">
                <span class="svg-container">
                    <svg-icon icon-class="user" />
                </span>
                <el-input
                    ref="username"
                    v-model="loginForm.username"
                    placeholder="请输入账号"
                    name="username"
                    type="text"
                    tabindex="1"
                    autocomplete="off"
                />
            </el-form-item>

            <!-- 密码 -->
            <el-form-item prop="password">
                <span class="svg-container">
                    <svg-icon icon-class="password" />
                </span>
                <el-input
                    :key="passwordType"
                    ref="password"
                    v-model="loginForm.password"
                    :type="passwordType"
                    placeholder="请输入密码"
                    name="password"
                    tabindex="2"
                    autocomplete="off"
                    @keyup.enter.native="handleLogin"
                />
                <span class="show-pwd" @click="showPwd">
                    <svg-icon
                        :icon-class="
                            passwordType === 'password' ? 'eye' : 'eye-open'
                        "
                    />
                </span>
            </el-form-item>

            <!-- 验证码 -->
            <el-form-item prop="password">
                <span class="svg-container">
                    <svg-icon icon-class="password" />
                </span>
                <el-input
                    ref="code"
                    v-model="loginForm.code"
                    placeholder="验证码"
                    name="code"
                    tabindex="3"
                    autocomplete="off"
                    @keyup.enter.native="handleLogin"
                />
            </el-form-item>
            <!-- 登录按钮 -->
            <div class="login-btn">
                <!-- 验证码 -->
                <img class="code-img" :src="codeObj.data" alt="" @click="refreshCode"/>
                <el-button
                    :loading="loading"
                    type="primary"
                    class="btn"
                    @click.native.prevent="handleLogin"
                    >登录</el-button
                >
            </div>
        </el-form>
    </div>
</template>

<script>
    import { validUsername } from '@/utils/validate'
    import { user_login } from '@/api/user'
    import { setToken } from '@/utils/auth'
    export default {
      name: 'Login',
      data() {
        const validateUsername = (rule, value, callback) => {
          if (!validUsername(value)) {
            callback(new Error('请输入正确的用户名'))
          } else {
            callback()
          }
        }
        const validatePassword = (rule, value, callback) => {
          if (value.length < 6) {
            callback(new Error('密码不能少于6位'))
          } else {
            callback()
          }
        }
        return {
          loginForm: {
            username: '',
            password: '',
            code: ''
          },
          codeObj: {},

          loginRules: {
            username: [
              { required: true, trigger: 'blur', validator: validateUsername }
            ],
            password: [
              { required: true, trigger: 'blur', validator: validatePassword }
            ]
          },
          loading: false,
          passwordType: 'password',
          redirect: undefined
        }
      },
      watch: {
        $route: {
          handler: function(route) {
            this.redirect = route.query && route.query.redirect
          },
          immediate: true
        }
      },

      created() {
        this.getCodeImg()
      },
      methods: {
        showPwd() {
          if (this.passwordType === 'password') {
            this.passwordType = ''
          } else {
            this.passwordType = 'password'
          }
          this.$nextTick(() => {
            this.$refs.password.focus()
          })
        },


        // 获取图片验证码
        getCodeImg() {
          this.sendGetRequest(this.api.captcha).then(res => {
            this.codeObj = res
          })
        },
        // 刷新验证码
        refreshCode(){
          this.getCodeImg()
        },
        // 登录
        handleLogin() {
          this.$refs.loginForm.validate(valid => {
            if (valid) {
              this.loading = true
              this.sendPostRequest(this.api.login, {
                ...this.loginForm,
                captcha_id: this.codeObj.id
              })
                .then(
                  res => {
                    console.log("登录成功",this.redirect,res.token);
                    setToken(res.token)
                    this.$router.push({ path: this.redirect || '/' })
                    this.loading = false
                  },
                  err => {
                    this.loading = false
                  }
                )
                .catch(() => {
                  this.loading = false
                })
            } else {
              console.log('error submit!!')
              return false
            }
          })
        }
      }
    }
</script>

<style lang="scss">
    /* 修复input 背景不协调 和光标变色 */
    /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

    $bg: #fff;
    $light_gray: #fff;
    $cursor: #333;

    @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
        .login-container .el-input input {
            color: $cursor;
        }
    }

    /* reset element-ui css */
    .login-container {
        .el-input {
            display: inline-block;
            height: 47px;
            width: 85%;

            input {
                background: transparent;
                border: 0px;
                -webkit-appearance: none;
                border-radius: 0px;
                padding: 12px 5px 12px 15px;
                color: #333;
                height: 47px;
                caret-color: $cursor;

                &:-webkit-autofill {
                    box-shadow: 0 0 0px 1000px #e5e5e5 inset !important;
                    -webkit-text-fill-color: $cursor !important;
                }
            }
        }

        .el-form-item {
            border: 1px solid rgba(255, 255, 255, 0.1);
            background: #e5e5e5;
            border-radius: 5px;
            color: #454545;
        }
    }
</style>

<style lang="scss" scoped>
    $bg: #2d3a4b;
    $dark_gray: #889aa4;
    $light_gray: #eee;

    .login-container {
        min-height: 100%;
        width: 100%;
        //background-color: #2d3a4b;
        background-image: url("../../assets/img/login_bk.svg");
        overflow: hidden;
        display: flex;
        justify-content: center;
        align-items: center;
        padding-bottom: 100px;
        .login-form {
            position: relative;
            width: 420px;
            max-width: 100%;
            padding: 35px 35px 0;
            margin: 0 auto;
            overflow: hidden;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0px 1px 7px #666;
        }

        .svg-container {
            padding: 6px 5px 6px 15px;
            color: #333;
            vertical-align: middle;
            width: 30px;
            display: inline-block;
        }

        .title-container {
            position: relative;

            .title {
                font-size: 26px;
                color: #333;
                margin: 0px auto 40px auto;
                text-align: center;
                font-weight: bold;
            }
        }

        .show-pwd {
            position: absolute;
            right: 10px;
            top: 7px;
            font-size: 16px;
            color: #333;
            cursor: pointer;
            user-select: none;
        }
    }

    .login-btn {
        width: 100%;
        display: flex;
        .code-img {
            width: 100px;
            height: 40px;
        }
        .btn {
            flex: 1;
            width: 50%;
            margin: 0 20px;
            margin-bottom: 30px;
        }
    }
</style>
